
// store the menu items
var menuitems = [];

var selectedmenu = 0;


/**
 * callback for get response from select of the menu item
 */
function selectmenuResponse(){
    if (httpRequest.readyState == 4) {
        if (httpRequest.status == '200') {
            setDebugMessage(httpRequest.responseText);
        }
        else {
            setDebugMessage(httpRequest.status + ' ' + httpRequest.statusText);
        }
        waitForResult = false;
    }
}



/**
 * set selected style
 */
function setSelectedMenu(number_){
    function setSelectedClass(number){
        menuitems[number].setAttribute('class', ((number < menuitems.length - 1) && 'menu' || 'menu_end') + ' menu_selected');
    }
    function setUnSelectedClass(number){
        menuitems[number].setAttribute('class', ((number < menuitems.length - 1) && 'menu' || 'menu_end') + ' menu');
    }
    var number = parseInt(number_, 0);
    if (number < menuitems.length) {
        setUnSelectedClass(selectedmenu);
        setSelectedClass(number);
        selectedmenu = number;
    }
}



/**
 * callback for select item of the left menu
 */
function selectmenu(){
    var menuid;
    var child = getFirstNode(this);
    while (child) {
        if (child.className == 'menuid') {
            menuid = child.firstChild.nodeValue;
            setSelectedMenu(menuid);
            if (!waitForResult && httpRequest) {
                var params = 'menuid=' + menuid;
                waitForResult = true;
                sendRequest('main', params, selectmenuResponse);
            }
            break;
        }
        child = child.nextSibling;
    }
    return false;
}

/**
 * initialize
 */
function leftmain(){
    // initialize debug panel
    spandebug = document.getElementById("spandebug");
    
    // initialize menu's items
    var leftmenu = document.getElementById("leftmenu");
    var child = getFirstNode(leftmenu);
    var i = 0;
    while (child) {
        if (child.nodeType == 1) {
            menuitems[i++] = child;
            child.onclick = selectmenu;
        }
        child = child.nextSibling;
    }
    
    // initialize menu
    selectedmenu = menuitems.length - 1;
    setSelectedMenu(0);
    selectedmenu = 0;
    // initialize request
    httpRequest = getHTTPRequestObject();
};




addEvent(window, 'load', leftmain)
